﻿using Rescue.Services;
using RescueApp.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Rescue.UI
{

    public partial class DispatchResult : Form
    {

        //public static string ConnectingStr = "Server=LAPTOP-GKEKHVVJ;Database=Todo;TrustServerCertificate=True;Integrated Security=True;";
        //RescueContext db = new RescueContext(ConnectingStr);
        private RescueContext db;
        private RescueApplyService srv;

        public DispatchResult()
        {
            InitializeComponent();
        }
        public DispatchResult(RescueContext db,RescueApplyService service)
        { 
            InitializeComponent();
            this.db = db;
            this.srv = service;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string message = comboBox1.Text;
            string input = textBox1.Text.Trim();
            //根据救援申请ID查询
            if (message == "救援申请ID")
            {
                // 输入转换为整数
                if (!int.TryParse(input, out int searchValue))
                {
                    MessageBox.Show("输入空白. 请输入查询内容.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                // 查询数据库
                var result = db.RescueDispatches.FirstOrDefault(rd => rd.RescueID == searchValue);
                if (result != null)
                {
                    // 如果找到匹配的记录，显示详细信息
                    MessageBox.Show($"救援派遣状态: 已派遣\n" +
                                    $"派遣单ID: {result.DispatchID}\n" +
                                    $"救援申请ID: {result.RescueID}\n" +
                                    $"救援车辆司机: {result.Driver}\n" +
                                    $"救援车辆车牌号: {result.LicenseID}\n" +
                                    $"到达现场时间: {result.ArrivedTime}\n" +
                                    $"故障原因: {result.Cause}\n" +
                                    $"救援费用: {result.Cost}",
                                    "派遣救援信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {

                    MessageBox.Show("没有找到派遣救援信息，请稍后再试.", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            if (message == "申请人电话")
            {
                //if (string.IsNullOrEmpty(input))
                //{
                //    MessageBox.Show("申请人电话不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    return ;
                //}

                int? rescueID = srv.GetRescueIDByPhone(input);
                var result = db.RescueDispatches.FirstOrDefault(rd => rd.RescueID == rescueID);
                if (result != null)
                {
                    // 如果找到匹配的记录，显示详细信息
                    MessageBox.Show($"救援派遣状态: 已派遣\n" +
                                    $"派遣单ID: {result.DispatchID}\n" +
                                    $"救援申请ID: {result.RescueID}\n" +
                                    $"救援车辆司机: {result.Driver}\n" +
                                    $"救援车辆车牌号: {result.LicenseID}\n" +
                                    $"到达现场时间: {result.ArrivedTime}\n" +
                                    $"故障原因: {result.Cause}\n" +
                                    $"救援费用: {result.Cost}",
                                    "派遣救援信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {

                    MessageBox.Show("没有找到派遣救援信息，请稍后再试.", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }






        }
    }
}
